package com.eorchis.module.reminduserlink.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.basedao.dao.impl.HibernateAbstractBaseDao;
import com.eorchis.module.news.domain.News;
import com.eorchis.module.remind.domain.Remind;
import com.eorchis.module.reminduserlink.dao.IRemindUserLinkDao;
import com.eorchis.module.reminduserlink.domain.RemindUserBean;
import com.eorchis.module.reminduserlink.domain.RemindUserLink;
import com.eorchis.core.basedao.query.condition.IPagingQueryCondition;
import com.eorchis.core.basedao.query.condition.builder.CompareType;
import com.eorchis.core.basedao.query.condition.builder.IConditionBuilder;
import com.eorchis.core.basedao.query.condition.builder.OrderType;
import com.eorchis.core.basedao.query.condition.builder.impl.DefaultQueryConditionBuilder;
import com.eorchis.core.basedao.query.mapper.MapperType;
import com.eorchis.core.basedao.query.mapper.ResultMapperFactory;
import com.eorchis.core.basedao.query.mapper.impl.BeanResultMapper;
import com.eorchis.core.page.commond.SortInfoBean;
import com.eorchis.core.ui.commond.IQueryCommond;
import com.eorchis.module.reminduserlink.ui.commond.RemindUserLinkQueryCommond;
import com.eorchis.module.reminduserlink.ui.commond.RemindUserLinkValidCommond;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: RemindUserLinkDaoImpl<br>
 * Description: 提醒用户关联表数据接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author RongSL
 * @createDate 2016-05-06
 * @version $Revision$
 */
@SuppressWarnings("unchecked")
@Repository(value = "com.eorchis.module.reminduserlink.dao.impl.RemindUserLinkDaoImpl")
public class RemindUserLinkDaoImpl extends HibernateAbstractBaseDao implements IRemindUserLinkDao {
	
	@Override
	public Class<? extends IBaseEntity> entityClass() {
		return RemindUserLink.class;
	}

	@Override
	public void queryConditionProcessor(
			IConditionBuilder defaultQueryConditionBuilder,
			IQueryCommond queryCommond) {
		RemindUserLinkQueryCommond remindUserLinkQueryCommond = (RemindUserLinkQueryCommond)queryCommond;
		defaultQueryConditionBuilder.setBaseQueryString("SELECT t FROM RemindUserLink t");
		defaultQueryConditionBuilder.addCondition("t.remindUserId", CompareType.IN, remindUserLinkQueryCommond.getSearchRemindUserIds());
		defaultQueryConditionBuilder.addCondition("t.lookState", CompareType.EQUAL,remindUserLinkQueryCommond.getSearchLookState());
		defaultQueryConditionBuilder.addCondition("t.remindValue", CompareType.LIKE,remindUserLinkQueryCommond.getSearchRemindValue());
		defaultQueryConditionBuilder.addCondition("t.replyUserId", CompareType.EQUAL,remindUserLinkQueryCommond.getSearchReplyUserid());
	}
	
	/**
	 * 查询未读提醒总数
	 * @Date 2016-06-24
	 * @author RongSL
	 * @return Integer
	 * @param RemindUserLinkQueryCommond
	 * */
	@Override
	public String[] findRemindUserNoReadCount(RemindUserLinkValidCommond commond) throws Exception{
		StringBuffer sb=new StringBuffer(" select (select count(1) from remind_user_link ru,remind r,base_basedata b " +
				"where  ru.remind_id=r.remind_id and r.remind_code=b.data_code and ru.look_state =:lookState " +
				"and ru.active_state = 1 and b.active_state=1 and ru.reply_user_id =:userId) rcount," +
				"(select count(1) from news_user_link n where n.read_state=:readState and n.active_state=1 and n.user_id=:userId) ncount from dual ");
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("readState", News.IS_N);
		params.put("lookState", RemindUserLink.LOOK_STATE_NO);
		params.put("userId", commond.getSearchReplyUserid());
		Object []object =(Object[]) this.executeFind(QueryStringType.SQL, sb.toString(), params).get(0);
		String o[]=new String[2];
		o[0]=(String)(object[0]+"");
		o[1]=(String)(object[1]+"");
		return o;
	}
	
	/**
	 * 查询提醒列表
	 * @Date 2016-06-12
	 * @author RongSL
	 * @return List<RemindUserLink>
	 * @param RemindUserLinkQueryCommond
	 * */
	@Override
	public List<RemindUserBean> findRemindUserList(RemindUserLinkQueryCommond commond){
 		StringBuffer hql=new StringBuffer("select t.remind.remindId as remindId,t.remindUserId as remindUserId," +
				"t.lookState as lookState,t.remindValue as remindValue,t.replyUserId as replyUserId,t.createDate as createDate," +
				"t.remind.remindType.dataName as remindType,t.remind.remindCategory.dataName as remindCategory  " +
				"FROM RemindUserLink t where t.activeState=1 and t.remind.remindType.activeState=1 ");
		IConditionBuilder defaultQueryConditionBuilder  = new DefaultQueryConditionBuilder();
		Map<String,Object> params=new HashMap<String,Object>();
		if(PropertyUtil.objectNotEmpty(commond.getSearchReplyUserid())){
			hql.append(" and  t.replyUserId=:userID ");
			params.put("userID",commond.getSearchReplyUserid());
		}
		
		//提醒类型
		if(PropertyUtil.objectNotEmpty(commond.getSearchRemindType())){
			hql.append(" and t.remind.remindType.dataCode=:type ");
			params.put("type", commond.getSearchRemindType());
		}
		
		//提醒分类
		if(PropertyUtil.objectNotEmpty(commond.getSearchRemindCategory())){
			hql.append(" and t.remind.remindCategory.dataCode=:category ");
			params.put("category",commond.getSearchRemindCategory());
		}
		
		//读取状态
		if(PropertyUtil.objectNotEmpty(commond.getSearchLookState())){
			hql.append(" and t.lookState=:lookState ");
			params.put("lookState", commond.getSearchLookState());
		}
		if (PropertyUtil.objectNotEmpty(commond.getSortInfos())) {
			SortInfoBean sortInfoBean = commond.getSortInfos();
			defaultQueryConditionBuilder.addSort(sortInfoBean);
		}else
			defaultQueryConditionBuilder.addSort("t.createDate", OrderType.DESC);
		
		defaultQueryConditionBuilder.setBaseQueryString(hql.toString());
		defaultQueryConditionBuilder.setResultObject(RemindUserBean.class);
		IPagingQueryCondition queryCondition = (IPagingQueryCondition) defaultQueryConditionBuilder.buliderQueryCondition();
		BeanResultMapper mapper = (BeanResultMapper) ResultMapperFactory.getInstance().getMapper(MapperType.Bean);
		mapper.setResultClass(RemindUserBean.class);
		queryCondition.setResultMapper(mapper);
		queryCondition.setParameters(params);
		queryCondition.setQueryStringType(QueryStringType.HQL);
		Long count=this.count(queryCondition);
		int firstResult = commond.calculate(count.longValue());
		return (List<RemindUserBean>)this.findList(queryCondition,firstResult,commond.getLimit());
	}
	
	/**
	 * 删除提醒用户关联
	 * @Date 2016-06-13
	 * @author RongSL
	 * */
	@Override
	public void deleteRemindUserLink(RemindUserLinkQueryCommond commond) throws Exception{
		StringBuffer HQL=new StringBuffer("delete from RemindUserLink r where  r.remindUserId in (:remindUserIds)");
		Map<String,Object> params=new HashMap<String,Object>();
		params.put("remindUserIds", commond.getSearchRemindUserIds());
		this.executeUpdate(QueryStringType.HQL, HQL.toString(), params);
	}
	
	/**
	 * 判断是否可以发送提醒
	 * @Date 2016-06-29
	 * @author RongSL
	 * */
	@Override
	public boolean seePermitState (String... par){
		try{
			StringBuffer HQL=new StringBuffer("select r.sendPermit from RemindUserLink r where r.remindClassIdCode=:code");
			Map<String,Object> params=new HashMap<String,Object>();
			params.put("code", par[0]);
			List<Object> list=(List<Object>) this.executeFind(QueryStringType.HQL, HQL.toString(), params);
			if(PropertyUtil.objectNotEmpty(list)){
				if(list.get(0).equals(RemindUserLink.SEND_PERMIT_YES)){
					return true;
				}else{
					return false;
				}
			}else{
				return true;
			}
		}catch(Exception e){
			System.out.println("判断是否可以发送提醒方法错误！");
		}
		return false;
	}
	
	/**
	 * 更新是否可以发送状态
	 * @Date 2016-06-29
	 * @author RongSL
	 * */
	@Override
	public void updateSendRemindState (String... par){
		try{
			StringBuffer sb=new StringBuffer(" update remind_user_link r set r.send_permit=:sendPermitState where r.remind_user_id in ( select rul.remind_user_id from remind_user_link rul " +
					" left join remind r on rul.remind_id=r.remind_id " +
					" where rul.remind_classidcode like :id and r.remind_code in (:type)) ");
			Map<String,Object> params=new HashMap<String,Object>();
			params.put("id", "%"+par[0]+"%");
			params.put("type", Remind.NoSparkRemind);
			params.put("sendPermitState", RemindUserLink.SEND_PERMIT_YES);
			this.executeUpdate(QueryStringType.SQL, sb.toString(), params);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("更新是否可以发送状态方法错误！");
		}
	}
	
	/**
	 * 将提醒更新为不可发送状态
	 * @Date 2016-06-29
	 * @author RongSL
	 * */
	@Override
	public void updateSendRemindStateNo(String... par){
		try{
			StringBuffer sb=new StringBuffer(" update remind_user_link r set r.send_permit=:sendPermitState where r.remind_classidcode=:code ");
			Map<String,Object> params=new HashMap<String,Object>();
			params.put("code",par[0]);
			params.put("sendPermitState", RemindUserLink.SEND_PERMIT_NO);
			this.executeUpdate(QueryStringType.SQL, sb.toString(), params);
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("将提醒更新为不可发送状态方法错误！");
		}
	}
}
